#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2022/10/20 17:17
# @Author : 闫峰
# @File    : yaml_func.py
import time

from selenium.webdriver import ActionChains
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from Common.basepage import BasePage
from Common.logger import Logger



class Yaml_func(BasePage):

    #点击
    def click_element(self,loc,img_name):

        ele = self.get_emement(loc,img_name)
        self.scroll_ele(loc, img_name)
        try:
            Logger.info("步骤：{}-点击元素：{}".format(img_name, loc))
            ele.click()
        except:
            self.save_page_shot()
            Logger.exception("点击元素{}失败".format(loc))
            raise

    #输入
    def input_text(self,loc,value,img_name):
        """
        :param loc:
        :param value:
        :param img_name:
        :param timeout:
        :param poll_fre:
        :return:
        """
        ele = self.get_emement(loc, img_name)
        self.scroll_ele(loc, img_name)
        try:
            Logger.info("步骤：{}-在元素{}输入{}".format(img_name, loc, value))
            ele.send_keys(value)
        except:
            self.save_page_shot()
            Logger.exception("元素{}输入{}异常".format(loc,value))
            raise

    #获取属性
    def get_ele_attribute(self,loc,attr_name,img_name):
        Logger.info("在{}获取元素{}的{}属性".format(img_name, loc,attr_name))

        ele = self.get_emement(loc, img_name)
        try:
            Logger.info("获取元素{}的{}属性".format(loc, attr_name))
            vaule = ele.get_attribute(attr_name)
        except:
            self.save_page_shot()
            Logger.exception("元素{}获取{}失败".format(loc,attr_name))
            raise
        else:
            Logger.info("属性为{}".format(vaule))
            return vaule

    # iframe切换
    def swith_to_iframe(self,loc,img_name):
        """
        :param loc: 可以是元祖 name  id 都是可以的
        :param img_name:
        :return:
        """
        # self.wait_ele_visible(loc, img_name)

        try:
            Logger.info("步骤：{},元素为{}".format(img_name,loc))
            WebDriverWait(self.driver,20).until(EC.frame_to_be_available_and_switch_to_it(loc))#等待+切换
        except:
            self.save_page_shot()
            Logger.exception("步骤：{}-元素{}切换错误".format(img_name,loc))
            raise

    #切换到父级
    def switch_parent_frame(self,img_name):
        Logger.info("切换到父iframe")
        try:
            self.driver.switch_to.parent_frame()
        except:
            self.save_page_shot()
            Logger.exception("步骤{}失败".format(img_name))

    #获取文本
    def get_ele_text(self,loc,img_name):
        """
        :param loc:
        :param img_name:
        :param timeout:
        :param poll_fre:
        :return:
        """

        # self.wait_ele_visible(loc, img_name, timeout, poll_fre)
        ele = self.get_emement(loc, img_name)
        try:
            Logger.info("步骤：{}-获取元素{}文本内容".format(img_name, loc))
            text = ele.text
        except:
            self.save_page_shot()
            Logger.exception("元素{}文本获取失败".format(loc))
            raise
        else:
            Logger.info("文本值为{}".format(text))
            return text

    #日期输入框输入日期
    def date_input(self,loc,value,img_name):
        """
        :param loc:
        :param value:
        :param img_name:
        :param timeout:
        :param poll_fre:
        :return:
        """

        ele = self.get_emement(loc,img_name)
        self.scroll_ele(loc, img_name)
        pha_js1 = """
        var a = arguments[0];
        a.readonly= false;
        a.value= arguments[1];
        """
        try:
            Logger.info("步骤：{}--元素{}输入{}".format(img_name,loc,value))
            self.driver.execute_script(pha_js1,ele,value)
        except:
            self.save_page_shot()
            Logger.exception("{}异常".format(img_name))
            raise


    #获取弹窗文本内容
    def get_bullet_frame_handle_text(self,img_name):
        time.sleep(1)
        try:
            Logger.info("步骤: {}".format(img_name))
            alert = self.driver.switch_to.alert
            text = alert.text
        except:
            self.save_page_shot()
            Logger.exception("{}失败".format(img_name))
            raise
        else:
            return text

    #清空输入框的值
    def clear_input(self,loc,img_name):

        ele = self.get_emement(loc,img_name)
        self.scroll_ele(loc, img_name)
        try:
            Logger.info("步骤：{}---清空元素{}".format(img_name,loc))
            ele.clear()
        except:
            self.save_page_shot()
            Logger.exception("{}操作失败".format(img_name))
            raise

    #非Select选择框通过js强制输入内容
    def no_select_js(self,loc,value,img_name):
        """
        使用场景如下：如果下拉框不是Select类，可以通过js代码修改元素的值
        js接受css定位
        :param csselement:
        :param value:
        :return:
        """

        ele = self.get_emement(loc, img_name)
        js = """
        var a = arguments[0];
        a.readonly= false;
        a.value= arguments[1];
        """
        try:
            self.driver.execute_script(js,ele,value)
        except:
            Logger.exception("执行js错误{}".format(img_name))
            raise

    #非Select选择框常规操作选择内容
    def no_select(self,loc,value,img_name):
        """

        :param loc1:
        :param loc2:
        :param img_name:
        :return:
        """

        self.click_element(loc,img_name)
        time.sleep(3)
        loc2 = (By.XPATH,value)
        self.scroll_ele(loc2,img_name)


        try:
            self.click_element(loc2,img_name)
            # self.driver.find_element(*loc2).click()
        except:
            Logger.exception("滚动滑动条点击元素失败")
            raise

    #鼠标移动到元素ele悬浮
    def actionChains_move_ele(self,loc,img_name):
        ele = self.get_emement(loc,img_name)
        try:
            ActionChains(self.driver).move_to_element(ele).perform()
        except:
            Logger.exception("{}操作失败".format(img_name))
            raise
